package com.miyu.system.service.sys.impl;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.miyu.common.pojo.cos.CosSysUser;
import com.miyu.common.utils.commonUtils.CommonUtils;
import com.miyu.system.dao.sys.CosSysUserDao;
import com.miyu.system.service.sys.CosSysUserService;
import org.springframework.stereotype.Service;


import javax.annotation.Resource;
import java.util.List;

/**
 * 系统用户服务impl
 *
 * @author MIYU
 * @date 2022/06/03
 */
@Service
public class CosSysUserServiceImpl  implements CosSysUserService {

    @Resource
    private CosSysUserDao cosSysUserDao;

    @Override
    public JSONObject editSysUser(JSONObject param) {
        CosSysUser cosSysUser = JSONObject.toJavaObject(param, CosSysUser.class);

        if(cosSysUser.getCosId() == null || cosSysUser.getCosId().equals("")){
            cosSysUser.setCosId(CommonUtils.getUUID());
        }
        cosSysUserDao.saveOrUpdateByNotNullProperties(cosSysUser);
        JSONObject rs = new JSONObject();
        rs.put("code", "1");
        rs.put("msg", "操作成功");
        return rs;
    }
    /**
     * 查询系统用户
     *
     * @param param 参数
     * @return {@code List<CosSysUser>}
     */
    @Override
    public List<CosSysUser> querySysUser(JSONObject param) {
        StringBuffer sql = new StringBuffer();
        sql.append(" from CosSysUser where 1=1 ");
        if(param.getString("cosName") != null && !param.getString("cosName").equals("")){
            sql.append(" and cosName like '%"+param.getString("cosName")+"%'");
        }
        List<CosSysUser> rs =  cosSysUserDao.queryListByHql(sql.toString());
        return rs;
    }

    /**
     * 删除系统用户
     *
     * @param param 参数
     * @return {@code JSONObject}
     */
    @Override
    public JSONObject delCosSysUser(JSONArray param) {
        List<CosSysUser> list = JSONArray.parseArray(param.toString(), CosSysUser.class);
        cosSysUserDao.deleteAll(list);
        JSONObject rs = new JSONObject();
        rs.put("code", "1");
        rs.put("msg", "操作成功");
        return rs;
    }
}
